#include "unix_proto.h"

int main (void)
{
	int sd;
	struct sockaddr_un local_addr;
	int new_sd;
	int cnt = 0;
	char buf[MSGSIZE] = {0};
	sd = socket (AF_UNIX, SOCK_STREAM, 0);
	if (-1 == sd) {
		perror ("socket()");
		exit (1);
	}
	
	local_addr.sun_family = AF_UNIX;
	strncpy (local_addr.sun_path, PATH, 108);
	int b = bind (sd, (struct sockaddr *)&local_addr, sizeof (local_addr));
	if (-1 == b) {
		perror ("bind()");
		goto ERROR;
	}
	listen (sd, 32);
	new_sd = accept (sd, NULL, NULL);
	if (-1 == new_sd) {
		perror ("accept()");
		goto ERROR;
	}

	while (1) {
		cnt = read (new_sd, buf, MSGSIZE);
		if (-1 == cnt) {
			perror ("read ()");
			goto ERROR;
		}
		if (0 == cnt) {
			break;
		}
		write (1, buf, cnt);
	}
	close (new_sd);
	close (sd);
	return 0;
ERROR:
	close (sd);
}
